import 'package:hive/hive.dart';

part 'task_model.g.dart';

/// 任务数据模型
/// 使用Hive注解来生成TypeAdapter
@HiveType(typeId: 3)
class TaskModel extends HiveObject {
  @HiveField(0)
  String id;

  @HiveField(1)
  String title;

  @HiveField(2)
  String description;

  @HiveField(3)
  bool isCompleted;

  @HiveField(4)
  DateTime createdAt;

  @HiveField(5)
  DateTime? completedAt;

  @HiveField(6)
  int priority; // 1: 低, 2: 中, 3: 高

  @HiveField(7)
  String category;

  @HiveField(8)
  DateTime? dueDate;

  @HiveField(9)
  List<String> tags;

  TaskModel({
    required this.id,
    required this.title,
    required this.description,
    this.isCompleted = false,
    required this.createdAt,
    this.completedAt,
    this.priority = 2,
    this.category = '通用',
    this.dueDate,
    this.tags = const [],
  });

  /// 从JSON创建TaskModel
  factory TaskModel.fromJson(Map<String, dynamic> json) {
    return TaskModel(
      id: json['id'] ?? '',
      title: json['title'] ?? '',
      description: json['description'] ?? '',
      isCompleted: json['isCompleted'] ?? false,
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'])
          : DateTime.now(),
      completedAt: json['completedAt'] != null
          ? DateTime.parse(json['completedAt'])
          : null,
      priority: json['priority'] ?? 2,
      category: json['category'] ?? '通用',
      dueDate: json['dueDate'] != null ? DateTime.parse(json['dueDate']) : null,
      tags: List<String>.from(json['tags'] ?? []),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'isCompleted': isCompleted,
      'createdAt': createdAt.toIso8601String(),
      'completedAt': completedAt?.toIso8601String(),
      'priority': priority,
      'category': category,
      'dueDate': dueDate?.toIso8601String(),
      'tags': tags,
    };
  }

  /// 复制并修改
  TaskModel copyWith({
    String? id,
    String? title,
    String? description,
    bool? isCompleted,
    DateTime? createdAt,
    DateTime? completedAt,
    int? priority,
    String? category,
    DateTime? dueDate,
    List<String>? tags,
  }) {
    return TaskModel(
      id: id ?? this.id,
      title: title ?? this.title,
      description: description ?? this.description,
      isCompleted: isCompleted ?? this.isCompleted,
      createdAt: createdAt ?? this.createdAt,
      completedAt: completedAt ?? this.completedAt,
      priority: priority ?? this.priority,
      category: category ?? this.category,
      dueDate: dueDate ?? this.dueDate,
      tags: tags ?? this.tags,
    );
  }

  /// 获取优先级文本
  String get priorityText {
    switch (priority) {
      case 1:
        return '低';
      case 2:
        return '中';
      case 3:
        return '高';
      default:
        return '中';
    }
  }

  /// 获取优先级颜色
  int get priorityColor {
    switch (priority) {
      case 1:
        return 0xFF4CAF50; // Green
      case 2:
        return 0xFFFF9800; // Orange
      case 3:
        return 0xFFF44336; // Red
      default:
        return 0xFFFF9800;
    }
  }

  /// 检查是否过期
  bool get isOverdue {
    if (dueDate == null || isCompleted) return false;
    return DateTime.now().isAfter(dueDate!);
  }

  /// 检查是否即将到期（24小时内）
  bool get isDueSoon {
    if (dueDate == null || isCompleted) return false;
    final now = DateTime.now();
    final difference = dueDate!.difference(now);
    return difference.inHours <= 24 && difference.inHours > 0;
  }

  /// 标记为完成
  TaskModel markAsCompleted() {
    return copyWith(isCompleted: true, completedAt: DateTime.now());
  }

  /// 标记为未完成
  TaskModel markAsIncomplete() {
    return copyWith(isCompleted: false, completedAt: null);
  }

  @override
  String toString() {
    return 'TaskModel(id: $id, title: $title, isCompleted: $isCompleted, priority: $priority, category: $category)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is TaskModel && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}
